package hos.ui.helper;

import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import hos.ui.drawable.DrawableUtils;
import hos.ui.R;

/**
 * <p>Title: ShapeHelper </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-06-26 14:59
 */
public class ShapeHelper {

    public static class DrawableType {
        public Drawable left;
        public Drawable right;
        public Drawable full;
        public Drawable source;

        public int shapeSpace = 12;

        public DrawableType(Drawable left, Drawable right, Drawable full, Drawable source, int shapeSpace) {
            this.left = left;
            this.right = right;
            this.full = full;
            this.source = source;
            this.shapeSpace = shapeSpace;
        }

        public Drawable newLeft() {
            return DrawableUtils.copyGradientDrawable(left);
        }

        public Drawable newRight() {
            return DrawableUtils.copyGradientDrawable(right);
        }

        public Drawable newFull() {
            return DrawableUtils.copyGradientDrawable(full);
        }

        public Drawable newSource() {
            return DrawableUtils.copyGradientDrawable(source);
        }
    }

    public static DrawableType loadFromAttributes(View view, AttributeSet attrs) {
        TypedArray typedArray =
                view.getContext().obtainStyledAttributes(attrs, R.styleable.ShapeView);
        try {
            int shapeType = typedArray.getInteger(R.styleable.ShapeView_shapeType, 0);
            if (shapeType == 0) {
                return null;
            }
            int shapeBorderWidth = typedArray.getInteger(R.styleable.ShapeView_shapeBorderWidth, 0);
            int shapeColor = typedArray.getColor(R.styleable.ShapeView_shapeColor, -1);
            int shapeBorderColor = typedArray.getColor(R.styleable.ShapeView_shapeBorderColor, -1);
            int shapeColorSelected = typedArray.getColor(R.styleable.ShapeView_shapeColorSelected, -1);
            float shapeRadius = typedArray.getFloat(R.styleable.ShapeView_shapeRadius, -1f);
            //px
            int shapeSpace = typedArray.getDimensionPixelSize(
                    R.styleable.ShapeView_shapeSpace,
                    12
            );
            // background 1
            // backgroundBorder 2
            // border 3
            Drawable drawable = null;
            if (shapeType == 1) {
                Drawable source = DrawableUtils.getBackground(shapeColor, shapeRadius);
                Drawable left = DrawableUtils.getBackground(shapeColor, shapeRadius,
                        0, shapeRadius, 0);
                Drawable right = DrawableUtils.getBackground(shapeColor, 0,
                        shapeRadius, 0, shapeRadius);
                Drawable full = DrawableUtils.getBackground(shapeColor, 0);
                if (shapeColorSelected == -1) {
                    return new DrawableType(left, right, full, source, shapeSpace);
                } else {
                    Drawable sourceSelected = DrawableUtils.getBackground(shapeColorSelected, shapeRadius);
                    Drawable leftSelected = DrawableUtils.getBackground(shapeColorSelected, shapeRadius,
                            0, shapeRadius, 0);
                    Drawable rightSelected = DrawableUtils.getBackground(shapeColorSelected, 0,
                            shapeRadius, 0, shapeRadius);
                    Drawable fullSelected = DrawableUtils.getBackground(shapeColorSelected, 0);
                    Drawable sourceUse = DrawableUtils.getDrawable(source, sourceSelected);
                    Drawable leftUse = DrawableUtils.getDrawable(left, leftSelected);
                    Drawable rightUse = DrawableUtils.getDrawable(right, rightSelected);
                    Drawable fullUse = DrawableUtils.getDrawable(full, fullSelected);
                    return new DrawableType(leftUse, rightUse, fullUse, sourceUse, shapeSpace);
                }
            } else if (shapeType == 2) {
                Drawable source = DrawableUtils.getBackgroundStroke(shapeColor, shapeBorderWidth, shapeBorderColor, shapeRadius);
                Drawable left = DrawableUtils.getBackgroundStroke(shapeColor, shapeBorderWidth, shapeBorderColor, shapeRadius,
                        0, shapeRadius, 0);
                Drawable right = DrawableUtils.getBackgroundStroke(shapeColor, shapeBorderWidth, shapeBorderColor, 0,
                        shapeRadius, 0, shapeRadius);
                Drawable full = DrawableUtils.getBackgroundStroke(shapeColor, shapeBorderWidth, shapeBorderColor, 0);
                if (shapeColorSelected == -1) {
                    return new DrawableType(left, right, full, source, shapeSpace);
                } else {
                    Drawable sourceSelected = DrawableUtils.getBackgroundStroke(shapeColorSelected, shapeBorderWidth, shapeBorderColor, shapeRadius);
                    Drawable leftSelected = DrawableUtils.getBackgroundStroke(shapeColorSelected, shapeBorderWidth, shapeBorderColor, shapeRadius,
                            0, shapeRadius, 0);
                    Drawable rightSelected = DrawableUtils.getBackgroundStroke(shapeColorSelected, shapeBorderWidth, shapeBorderColor, 0,
                            shapeRadius, 0, shapeRadius);
                    Drawable fullSelected = DrawableUtils.getBackgroundStroke(shapeColorSelected, shapeBorderWidth, shapeBorderColor, 0);
                    Drawable sourceUse = DrawableUtils.getDrawable(source, sourceSelected);
                    Drawable leftUse = DrawableUtils.getDrawable(left, leftSelected);
                    Drawable rightUse = DrawableUtils.getDrawable(right, rightSelected);
                    Drawable fullUse = DrawableUtils.getDrawable(full, fullSelected);
                    return new DrawableType(leftUse, rightUse, fullUse, sourceUse, shapeSpace);
                }
            } else if (shapeType == 3) {
                Drawable source = DrawableUtils.getBorderRadius(shapeColor, shapeBorderWidth, shapeRadius);
                Drawable left = DrawableUtils.getBorderRadius(shapeColor, shapeBorderWidth, shapeRadius,
                        0, shapeRadius, 0);
                Drawable right = DrawableUtils.getBorderRadius(shapeColor, shapeBorderWidth, 0,
                        shapeRadius, 0, shapeRadius);
                Drawable full = DrawableUtils.getBorderRadius(shapeColor, shapeBorderWidth, 0);
                if (shapeColorSelected == -1) {
                    return new DrawableType(left, right, full, source, shapeSpace);
                } else {
                    Drawable sourceSelected = DrawableUtils.getBorderRadius(shapeColorSelected, shapeBorderWidth, shapeRadius);
                    Drawable leftSelected = DrawableUtils.getBorderRadius(shapeColorSelected, shapeBorderWidth, shapeRadius,
                            0, shapeRadius, 0);
                    Drawable rightSelected = DrawableUtils.getBorderRadius(shapeColorSelected, shapeBorderWidth, 0,
                            shapeRadius, 0, shapeRadius);
                    Drawable fullSelected = DrawableUtils.getBorderRadius(shapeColorSelected, shapeBorderWidth, 0);
                    Drawable sourceUse = DrawableUtils.getDrawable(source, sourceSelected);
                    Drawable leftUse = DrawableUtils.getDrawable(left, leftSelected);
                    Drawable rightUse = DrawableUtils.getDrawable(right, rightSelected);
                    Drawable fullUse = DrawableUtils.getDrawable(full, fullSelected);
                    return new DrawableType(leftUse, rightUse, fullUse, sourceUse, shapeSpace);
                }
            }
            return null;
        } finally {
            typedArray.recycle();
        }
    }
}
